package com.github.glomadrian.materialanimatedswitch.painter;

import com.github.glomadrian.materialanimatedswitch.MaterialAnimatedSwitchState;
import com.github.glomadrian.materialanimatedswitch.utils.PixelMapUtils;
import ohos.agp.render.BlendMode;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Texture;
import ohos.agp.utils.Color;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;

public class ShaderPainter implements SwitchInboxPinnedPainter {
    private Paint maskPaint;
    private int height;
    private int width;
    private PixelMap mask;

    public ShaderPainter() {
        maskPaint = new Paint();
        maskPaint.setColor(Color.TRANSPARENT);
        maskPaint.setBlendMode(BlendMode.CLEAR);
        maskPaint.setStyle(Paint.Style.FILL_STYLE);
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.drawLine(0, height / 2, width, height / 2, maskPaint);
    }

    @Override
    public int getColor() {
        return 0;
    }

    @Override
    public void setColor(int color) {
    }

    @Override
    public void onSizeChanged(int height, int width) {
        this.height = height;
        this.width = width;
        createMask(height, width, 10);
    }

    @Override
    public void setState(MaterialAnimatedSwitchState state) {
    }

    private void createMask(int w, int h, int radius) {
        if (mask != null) {
            mask.release();
        }

        mask = PixelMapUtils.createBitmap(w, h, PixelFormat.ARGB_8888);
        Canvas maskCanvas = new Canvas(new Texture(mask));
        maskCanvas.drawCircle(w, h, radius, maskPaint);
    }
}
